<?php
/**
 * | ---------------------------------------------------------------------------------------------------
 * | ProjectName: mvc
 * | ---------------------------------------------------------------------------------------------------
 * | Author：johnxu <fsyzxz@163.com>
 * | ---------------------------------------------------------------------------------------------------
 * | Home: https://www.xfjpeter.cn
 * | ---------------------------------------------------------------------------------------------------
 * | Data: 201907162019-07-16
 * | ---------------------------------------------------------------------------------------------------
 * | Desc:
 * | ---------------------------------------------------------------------------------------------------
 */

namespace app\admin\controller;

use app\common\libs\Tree;

class Menu extends Base
{
    // 获取菜单列表[根据登录的用户权限]
    public function index()
    {
        $user = model('admin')->where('username', $this->user->username)->find();
        if (!$user) {
            $this->fail(200, 1, '用户不存在');
        }

        // 查询用户绑定的角色，以及角色所在的规则
        $adminRole = db('admin_role')->where('user_id', $user->id)->find();
        if (!$adminRole) {
            $this->fail(200, 1, '该用户没有绑定角色');
        }

        // 查询角色对应的菜单
        $rules     = model('role')->where('id', $adminRole['role_id'])->value('rules');
        $rules     = model('rule')->where('id', 'in', $rules)->where(['status' => 1, 'is_menu' => 1, 'is_show' => 1])->select();
        $rulesTree = Tree::getRule($rules);

        $this->ok(200, $rulesTree);
    }
}